package org.aplikator.client.shared.rpc.marshaller;

import org.aplikator.client.shared.data.ClientContext;
import org.aplikator.client.shared.data.Record;
import org.aplikator.client.shared.descriptor.FunctionDTO;
import org.aplikator.client.shared.rpc.ParameterWrapperRunFunction;
import org.jboss.errai.marshalling.client.api.Marshaller;
import org.jboss.errai.marshalling.client.api.MarshallingSession;
import org.jboss.errai.marshalling.client.api.annotations.ClientMarshaller;
import org.jboss.errai.marshalling.client.api.annotations.ServerMarshaller;
import org.jboss.errai.marshalling.client.api.json.EJObject;
import org.jboss.errai.marshalling.client.api.json.EJValue;
import org.jboss.errai.marshalling.client.marshallers.AbstractNullableMarshaller;

@ClientMarshaller(ParameterWrapperRunFunction.class)
@ServerMarshaller(ParameterWrapperRunFunction.class)
public class ParameterWrapperRunFunctionMarshaller extends AbstractNullableMarshaller<ParameterWrapperRunFunction> {

	/*@Override
	public Class<ParameterWrapperRunFunction> getTypeHandled() {
		return ParameterWrapperRunFunction.class;
	}*/

	@Override
	public ParameterWrapperRunFunction[] getEmptyArray() {
		return new ParameterWrapperRunFunction[0];
	}

	@Override
	public ParameterWrapperRunFunction doNotNullDemarshall(EJValue o,
			MarshallingSession ctx) {
		if (o.isObject() != null) {
			EJObject ejObject = o.isObject();
			ParameterWrapperRunFunction pm = new ParameterWrapperRunFunction();
			if (DemarshallingUtils.containsAndNotNull(ejObject,"function")) {
				Marshaller<Object> marshInstance = ctx.getMarshallerInstance(FunctionDTO.class.getName());
				pm.setFunction((FunctionDTO) marshInstance.demarshall(ejObject.get("function"), ctx));
			} 
			if (DemarshallingUtils.containsAndNotNull(ejObject,"clientContext")) {
				Marshaller<Object> marshInstance = ctx.getMarshallerInstance(ClientContext.class.getName());
				pm.setClientContext((ClientContext) marshInstance.demarshall(ejObject.get("clientContext"), ctx));
			}		
			if (DemarshallingUtils.containsAndNotNull(ejObject,"functionParameters")) {
				Marshaller<Object> marshInstance = ctx.getMarshallerInstance(Record.class.getName());
				pm.setFunctionParameters((Record) marshInstance.demarshall(ejObject.get("functionParameters"), ctx));
			}
			return pm;
		} else return null;
	}

	@Override
	public String doNotNullMarshall(ParameterWrapperRunFunction o,
			MarshallingSession ctx) {
		StringBuilder builder = new StringBuilder().append('{');

		MarshallingUtils.key("function", builder).append(':');
		Marshaller<Object> marshInstance = ctx.getMarshallerInstance(FunctionDTO.class.getName());
		builder.append(marshInstance.marshall(o.getFunction(), ctx)).append(',');

		MarshallingUtils.key("clientContext", builder).append(':');
		marshInstance = ctx.getMarshallerInstance(ClientContext.class.getName());
		builder.append(marshInstance.marshall(o.getClientContext(), ctx)).append(',');

		MarshallingUtils.key("functionParameters", builder).append(':');
		marshInstance = ctx.getMarshallerInstance(Record.class.getName());
		builder.append(marshInstance.marshall(o.getFunctionParameters(), ctx));

		builder.append('}');
		return builder.toString();
	}
}
